Skip to main content
Version: Latest (Core: 0.60.x, Azure: 0.46.x)

Client hierarchy

This page documents the default client hierarchy behavior as well as how to customize clients. For an overview of the setup, please visit the previous page.

JS RLC is not in the business of customization. it will ignore client.tsp and the follow scenarios will not have impact on the JS RLC user experiences. In this context, TypeScript part means JS Modular Emitter.

Default behavior​

By default, each namespace with @service decorator will be generated as a root client. The name for that client will be the namespace name concatenating Client as suffix.

Other nested namespacess and interfaces of each root client will be generated as operation groups with hierarchy.

Different language's code generator will have different way to organize clients and operation groups. Please refer the following examples.

Single client​

@service({
title: "Pet Store",
version: "v1",
})
namespace PetStore;

@route("/feed")
op feed(): void;

@route("/op2")
op pet(): void;

Client with one-layer child operation groups​

@service({
title: "Pet Store",
version: "v1",
})
namespace PetStore;

@route("/dogs")
interface Dogs {
feed(): void;
pet(): void;
}

@route("/cats")
namespace Cats {
op feed(): void;
op pet(): void;
}

Client with multi-layer child operation group​

@service({
title: "Pet Store",
version: "v1",
})
namespace PetStore;

@route("/info")
op info(): void;

@route("/billings")
interface Billings {
@route("history")
history(): void;
}

@route("/pets")
namespace Pets {
@route("info")
op info(): void;

@route("/actions")
interface Actions {
feed(): void;
pet(): void;
}
}

@route("/actions")
interface Actions {
open(): void;
close(): void;
}

Customizations​

Customization SHOULD always be done in a file called client.tsp along with the main.tsp. If there is any customizations including @client and @operationGroup, client hierarchy will only be inferred from them. The logic defined in the default behaviors will not take affect anymore.

For this section, we will assume that you have service called PetStore in the namespace PetStore, defining the two operations feed and pet.

Renaming the single client​

This can be achieved with the augment operator and the emitter package

import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;

@@clientName(PetStore, "PetStoreGreatClient");

Splitting the operations into two clients​

Two clients that separate the operations can be declared using the @client decorator from typespec-client-generator-core:

import "./main.tsp"
import "@azure-tools/typespec-client-generator-core"

using Azure.ClientGenerator.Core;

namespace Customizations; # The actual name here doesn't matter and is here for organization purposes only

@client({
name: "FoodClient",
service: PetStoreNamespace
})
interface Client1 {
feed is PetStoreNamespace.feed;
}

@client({
name: "PetActionClient",
service: PetStoreNamespace
})
interface Client2 {
pet is PetStoreNamespace.pet;
}

One client and two operation groups​

Two clients that separate the operations can be declared using the @client decorator and the @operationGroup decorator from typespec-client-generator-core:

import "./main.tsp"
import "@azure-tools/typespec-client-generator-core"

using Azure.ClientGenerator.Core;

@client({
name: "PetStoreClient",
service: PetStoreNamespace
})
namespace Customizations; # The actual name here doesn't matter and is here for organization purposes only

@operationGroup
interface OpGrp1{
feed is PetStoreNamespace.feed
}

@operationGroup
interface OpGrp2 {
pet is PetStoreNamespace.pet
}

Splitting the operations in two clients and have clients in different namespace​

Two clients that separates the operations can be declared using the client decorator of typespec-client-generator-core:

import "./main.tsp"
import "@azure-tools/typespec-client-generator-core"

using Azure.ClientGenerator.Core;

namespace Customizations; # The actual name here doesn't matter and is here for organization purposes only

@client({
name: "FoodClient",
service: PetStoreNamespace
})
interface Client1 {
feed is PetStoreNamespace.feed
}

@client({
name: "SubNamespace.PetActionClient",
service: PetStoreNamespace
})
interface Client2 {
pet is PetStoreNamespace.pet
}